package lumis.content.core;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lumis.content.ContentManagerFactory;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.serviceinterface.IServiceInterfaceRequest;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Content utility class.
 * 
 * @hidden
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 4.2.0
 */
public class ContentUtil
{
	private static ILogger logger = LoggerFactory.getLogger(ContentUtil.class);
	
	/**
	 * Gets the itemId parameter for a request, trying to be most backward
	 * compatible with the 4.1 implementation logic.
	 * <p>
	 * Uses the value of {@link PortalRequestParameters#PAGE_PARAMETER_ITEM_ID}
	 * as contentId, tries to discover the most appropriated 
	 * contentVersion.itemId (data primary key) and returns it.
	 * 
	 * @param request the request.
	 * @return the itemId, or <code>null</code> if it could not be obtained.
	 * @since 4.2.0
	 */
	public static String getOldItemIdParameter(IServiceInterfaceRequest request) throws PortalException
	{
		String contentId = request.getParameter(PortalRequestParameters.PAGE_PARAMETER_ITEM_ID);
		if (contentId != null)
		{
			try
			{
				Content content = ContentManagerFactory.getContentManager().getContent(contentId);
				Locale locale = SessionConfig.getCurrentSessionConfig().getLocale();
				if (request.getServiceInterfaceConfig().isAdministration())
				{
					ContentLocale contentLocale = content.getContentLocale(locale, true);
					if (contentLocale != null)
						return contentLocale.getActiveVersion().getItemId();
				}
				else
				{
					ContentVersion version = ContentManagerFactory.getContentManager().getPublishedContentVersion(contentId, null);
					return version.getItemId();
				}
			}
			catch (PortalObjectNotFoundException e)
			{
				return null;
			}
			catch (Exception e)
			{
				logger.warn("Exception while trying to find itemId from contentId", e);
				return null;
			}
		}
		
		return null;
	}
	
	/**
	 * Normalizes a tag name. The normalization applies rules that are used to identify
	 * if two tags are considered equal and may not coexist in a same tag repository.
	 * @param name the tag name.
	 * @return the normalized tag name.
	 * @since 6.0.0
	 */
	public static String normalizeTagName(String name)
	{
		return name.toUpperCase(Locale.ENGLISH);
	}
	
	/**
	 * Returns true if the tag name has '{', otherwise returns false.
	 * 
	 * @return true if the tag name has '{', otherwise returns false.
	 * @since 6.0.0
	 */
	public static boolean isValidTagName(String name)
	{
		boolean isValid;

		Pattern p = Pattern.compile("\\{");
		Matcher m = p.matcher(name);
		if (m.find())
			isValid = false;
		else
			isValid = true;
		
		return isValid;
	}
}
